package com.igroupes.ovineherd.model.attribute.number;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.NumberStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.igroupes.ovineherd.util.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.ArrayList;
import java.util.List;

/**
 * 数字
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class NumberBasicAttribute extends BasicAttribute {

    public static final Integer MAXAGE = 99999;
    public static final Integer MINAGE = 0;
    public static final String MINLABEL = "最小值";
    public static final String MAXLABEL = "最大值";
    protected NumberStyle minValue; //最小值
    protected NumberStyle maxValue; //最大值

    protected NumberBasicAttribute() {
        super();
        this.minValue = new NumberStyle(MINLABEL, MINAGE, false, MINAGE);
        this.maxValue = new NumberStyle(MAXLABEL, MAXAGE, false, MINAGE, MAXAGE);
    }

    public Boolean verifyField(NumberBasicAttribute textBasicAttribute) {
        super.verifyField(textBasicAttribute);
        new NumberStyle().verify(textBasicAttribute.getMinValue());
        new NumberStyle().verify(textBasicAttribute.getMaxValue());
        return true;
    }
    public Boolean verifyFieldV2(NumberBasicAttribute basic) {
        super.verifyFieldV2(basic);
        new NumberStyle().verify(new NumberStyle(MINLABEL, basic.getMinValue().getValue(), false, MINAGE));
        new NumberStyle().verify( new NumberStyle(MAXLABEL, basic.getMaxValue().getValue(), false, MINAGE, MAXAGE));
        return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        NumberBasicAttribute textBasicAttribute = (NumberBasicAttribute) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData, dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        fieldQuery.setType(MySqlTypeKey.BIGINT.getCode());
        fieldQuery.setLength(Convert.toStr(textBasicAttribute.getMaxValue().getValue()).length());

        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("minValue", textBasicAttribute.getMinValue().getValue());
        jsonObject.set("maxValue", textBasicAttribute.getMaxValue().getValue());
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0, fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("minValue", new NumberStyle(MINLABEL, modelDataJsonAttributeQuery.getJson().get("minValue"), false, MINAGE));
        jsonObject.set("maxValue", new NumberStyle(MAXLABEL, modelDataJsonAttributeQuery.getJson().get("maxValue"), false, MINAGE, MAXAGE));
        return jsonObject;
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {

        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        if(ObjectUtil.isNotNull(value)){

            Integer data = StringUtils.strToAnyType(value.toString() , Integer.class);
            JSONObject jsonObject = modelAttributeEntity.getJson();
            if(ObjectUtil.isNotNull(jsonObject.get("minValue"))) {
                Integer minValue = Convert.toInt(jsonObject.get("minValue"));
                if (data < minValue) {
                    throw new ModelException(String.format("%s不能小于"+minValue, modelAttributeEntity.getName()));
                }
            }
            if( ObjectUtil.isNotNull(jsonObject.get("maxValue"))) {
                Integer maxValue = Convert.toInt(jsonObject.get("maxValue"));
                if ( data > maxValue) {
                    throw new ModelException(String.format("%s不能大于"+maxValue, modelAttributeEntity.getName()));
                }
            }
        }
        FieldApiResponse fieldApiResponse = new FieldApiResponse();
        fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
        fieldApiResponse.setValue(value);
        fieldApiResponse.setIsStr(false);
        fieldApiResponse.setType(MySqlTypeKey.INT.getCode());
        fieldApiResponses.add(fieldApiResponse);
        return fieldApiResponses;
    }

    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.NUMBER.getCode(), true, true, true);
    }
}
